<?php
// a Client RPC library to DB and a Session Counter

include_once("../../php/db/MySQLiWrapper.php");

class clientServices
	{

	public function clientServices()
	    {
	    $this->methodTable = array(
		"get_game_data" => array(
		    "description" => "query for game data rescordset",
		    "access"    => "remote",
		    "arguments" => array(arg1),
		    "returntype"=> "recordSet"
		),
		"get_players_data" => array(
		    "description" => "query for players data rescordset",
		    "access"    => "remote",
		    "arguments" => array(arg1),
		    "returntype"=> "recordSet"
		),
		"get_board_data_by_id" => array(
		    "description" => "query for board data by id rescordset",
		    "access"    => "remote",
		    "arguments" => array(arg1),
		    "returntype"=> "recordSet"
		),
		"get_properties_list" => array(
		    "description" => "query for properties list rescordset",
		    "access"    => "remote",
		    "arguments" => array(arg1),
		    "returntype"=> "recordSet"
		),
// FUI
		"get_player_by_name" => array(
   		    "description" => "query for player data by name",
		    "access"    => "remote",
	   	    "arguments" => array(arg1),
		    "returntype"=> "recordSet"
		),
		"get_player_data_by_id" => array(
		    "description" => "query for player data by id",
		    "access"    => "remote",
		    "arguments" => array(arg1),
		    "returntype"=> "recordSet"
		),
		"verify_user_by_name" => array(
		    "description" => "query for player data by id",
		    "access"    => "remote",
		    "arguments" => array(arg1),
		    "returntype"=> "recordSet"
		),
		"get_langs_list" => array(
		    "description" => "get list of supported languages",
		    "access"    => "remote",
		    "arguments" => null,
		    "returntype"=> "recordSet"
		),
		"add_new_player_wrapper" => array(
		    "description" => "add new player",
		    "access"    => "remote",
		    "arguments" => array(arg1),
		    "returntype"=> "recordSet"
		),
			
		"get_templates_list" => array(
		    "description"    => "Get template list",
		    "access"    => "remote",
		    "arguments" => null,
		    "returns"=> "recordSet"
		)
	    );

	    // Check if the session is available or create it.
            if (!isset($_SESSION['count']))
		{
                $_SESSION['count'] = 0;
        	}
	    }

	/* general usage function */

	function mquery($query)
	    {
	    $dbconn = new MySQLiWrapper();
	    //$dbconn->connect();
	    $result = $dbconn->MQuery($query);
	    $dbconn->disconnect();
	    
	    return $result;
	    }
	function query($query)
	    {
	    $dbconn = new MySQLiWrapper();
	    //$dbconn->connect();
	    $result = $dbconn->Query($query);
	    $dbconn->disconnect();
	    
	    return $result;
	    }

	/* the AMFPHP RPC interface */
	
	public function get_game_data($gameId)
	    {
	    //@TP - try stored procedure
	    $query = "call mp_get_game_info($gameId)";
	    
	    return ($this->mquery($query));
	    }

	/* get all players data for game using SP */
	public function get_players_data($gameId)
	    {
	    $query = "call mp_get_gathered_players_list($gameId)";

	    return $this->mquery($query);
	    }
	/* get list of game items */
	public function get_properties_list($gameId)
	    {
	    $query = "call mp_get_game_items($gameId)";
	    return $this->mquery($query);
	    }

	// Used to increment the session variable count.
    	public function increment() 
	    {
            $_SESSION['count']++;
            return $_SESSION['count'];
    	    }
	// used to destroy the session variable and start over.
    	public function unregister()
	    {
            unset($_SESSION['count']);
    	    return true;
    	    }


	public function get_player_data_by_id($id)
	    {
	    $query = "SELECT * FROM users WHERE id='$id'";
	    return $this->query($query);
	    }
	public function verify_user_by_name($user, $pwd)
	    {
	    $query = "SELECT * FROM users WHERE `name`='$user' AND `passwd`='$pwd'"; #PASSWORD('$pwd')";  Until IGF will fix DB to store real PASSWD field...

	    return $this->query($query);
	    }
	public function get_player_by_name($name) 
	    {
	    $query = "SELECT * FROM users WHERE name='$name'";

	    return $this->query($query);
	    }
	public function get_langs_list()
	    {
	    $query = "SELECT * FROM languages";

	    return $this->query($query);
	    }
	public function add_new_player_wrapper($user_data)
	    {
	    $query = "INSERT INTO `users` (`id` ,`name` ,`passwd` ,`logo` ,`f_name` ,`l_name` ,`gender` ,`age` , `adrs_country` ,`adrs_city` ,`adrs_street` ,`adrs_zip` ,`lang_id` ,`email` ,`phone1` ,`phone2` , `reg_date` ,`status`) VALUES (NULL , '".$user_data[0]['name']."', PASSWORD('".$user_data[0]['password'].  "'), NULL , '".$user_data[0]['fname']."', '".$user_data[0]['lname']."', '".$user_data[0]['gender']."', '".$user_data[0]['age']. "', '".$user_data[0]['country']."', '".$user_data[0]['city']."', '".$user_data[0]['street']."', '".$user_data[0]['zipcode']. "', '".$user_data[0]['lang']."', '".$user_data[0]['email']."', '".$user_data[0]['phone']."', '".$user_data[0]['phone2']."', CURRENT_TIMESTAMP , 'PENDING');";

	    return $this->query($query);
	    }
	public function get_templates_list()
	    {
	    $query = "call mp_get_tamplates_list";
	    
	    return $this->mquery($query);
	    }

	public function destroy()
	    {
            session_destroy();
    	    return true;
    	    }
	}
?>
